package com.ruoyi.tfjData.service.impl;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSON;
import com.deepoove.poi.data.PictureRenderData;
import com.ruoyi.common.code.ServiceHttpCode;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.pad.ChartLineDTO;
import com.ruoyi.common.utils.*;
import com.ruoyi.common.utils.calculate.BezierCurveUtil;
import com.ruoyi.common.utils.calculate.MotorUtils;
import com.ruoyi.common.utils.jkalman.JKalmanUtils;
import com.ruoyi.task.domain.entity.DataTaskEntity;
import com.ruoyi.task.service.IDataTaskService;
import com.ruoyi.tfjData.domain.pad.TfjTaskResqDTO;
import com.ruoyi.tfjData.domain.plat.DataTfj;
import com.ruoyi.tfjData.domain.plat.TfjChartRespDTO;
import com.ruoyi.tfjData.domain.report.TfjReportReqDTO;
import com.ruoyi.tfjData.mapper.DataTfjMapper;
import com.ruoyi.tfjData.service.IDataTfjService;
import com.ruoyi.tfjData.utils.BiaokuangUtils;
import com.ruoyi.tfjData.utils.GongkuangUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


/**
 * 通风机测试结果Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-27
 */
@Service
public class DataTfjServiceImpl implements IDataTfjService
{
    @Autowired(required = false)
    private DataTfjMapper dataTfjMapper;

    @Autowired(required = false)
    private IDataTaskService dataTaskService;

    /**
     * 查询通风机测试结果
     * 
     * @param tfjId 通风机测试结果主键
     * @return 通风机测试结果
     */
    @Override
    public DataTfj selectDataTfjByTfjId(Long tfjId)
    {
        return dataTfjMapper.selectDataTfjByTfjId(tfjId);
    }

    /**
     * 查询通风机测试结果列表
     * 
     * @param dataTfj 通风机测试结果
     * @return 通风机测试结果
     */
    @Override
    public List<DataTfj> selectDataTfjList(DataTfj dataTfj)
    {
        return dataTfjMapper.selectDataTfjList(dataTfj);
    }

    /**
     * 新增通风机测试结果
     * 
     * @param dataTfj 通风机测试结果
     * @return 结果
     */
    @Override
    public int insertDataTfj(DataTfj dataTfj)
    {
        dataTfj.setCreateTime(DateUtils.getNowDate());
        return dataTfjMapper.insertDataTfj(dataTfj);
    }

    /**
     * 修改通风机测试结果
     * 
     * @param dataTfj 通风机测试结果
     * @return 结果
     */
    @Override
    public int updateDataTfj(DataTfj dataTfj)
    {
        dataTfj.setUpdateTime(DateUtils.getNowDate());
        return dataTfjMapper.updateDataTfj(dataTfj);
    }

    /**
     * 批量删除通风机测试结果
     * 
     * @param tfjIds 需要删除的通风机测试结果主键
     * @return 结果
     */
    @Override
    public int deleteDataTfjByTfjIds(Long[] tfjIds)
    {
        return dataTfjMapper.deleteDataTfjByTfjIds(tfjIds);
    }

    /**
     * 删除通风机测试结果信息
     * 
     * @param tfjId 通风机测试结果主键
     * @return 结果
     */
    @Override
    public int deleteDataTfjByTfjId(Long tfjId)
    {
        return dataTfjMapper.deleteDataTfjByTfjId(tfjId);
    }

    /**
     * 根据任务Id查询通风机测试结果
     *
     * @param dataTaskId
     * @return
     */
    @Override
    @Async("threadPoolKy")
    public Future<List<DataTfj>> selectAllByDataTaskId(Long dataTaskId) {
        return new AsyncResult<>(dataTfjMapper.selectAllByDataTaskId(dataTaskId));
    }

    /**
     * 根据工况点获取通风机性能曲线图
     *
     * @param tfjId
     */
    @Override
    public TfjChartRespDTO getChartData(Long[] tfjId) {
        TfjChartRespDTO tfjChartRespDTO = new TfjChartRespDTO();
        DecimalFormat df2 = new DecimalFormat("####0.00");
        if (tfjId == null || tfjId.length < 4) {
            tfjChartRespDTO.setCode(ServiceHttpCode.TFJ_CHART_POINT_LESS.getCode());
            tfjChartRespDTO.setMsg(ServiceHttpCode.TFJ_CHART_POINT_LESS.getMsg());
            return tfjChartRespDTO;
        }
        // 查询数据集合
        List<DataTfj> tfjResults = dataTfjMapper.selectAllDataTfjByTfjIds(tfjId);
        // 去掉集合中风量为空的点
        List<DataTfj> dataFilterList = tfjResults.stream().filter(item -> item.getBkFl() != null).collect(Collectors.toList());
        // 去掉风量相同的数据点
        Set<DataTfj> tfjResultSet = new TreeSet<>(Comparator.comparing(DataTfj::getBkFl));
        tfjResultSet.addAll(dataFilterList);
        ArrayList<DataTfj> results = new ArrayList<>(tfjResultSet);

        if (results.size() < 4) {
            tfjChartRespDTO.setCode(ServiceHttpCode.TFJ_CHART_VALID_POINT_LESS.getCode());
            tfjChartRespDTO.setMsg(ServiceHttpCode.TFJ_CHART_VALID_POINT_LESS.getMsg());
            return tfjChartRespDTO;
        }
        // 按瞬时流量排序
        List<DataTfj> collect = results.stream().sorted(Comparator.comparing(dataTfj -> {return Float.parseFloat(dataTfj.getBkFl());})).collect(Collectors.toList());
        float maxFl = Float.parseFloat(collect.get(0).getBkFl()), minFl = Float.parseFloat(collect.get(0).getBkFl());
        //x轴标况风量
        List<Float> flList = new ArrayList<>();
        //y轴风压
        List<Float> fyFList = new ArrayList<>();
        List<Float> fyNiheFList = new ArrayList<>();
        //y轴功率
        List<Float> glFList = new ArrayList<>();
        List<Float> glNiheFList = new ArrayList<>();
        //y轴效率
        List<Float> xlFList = new ArrayList<>();
        List<Float> xlNiheFList = new ArrayList<>();
        for (DataTfj item : collect) {
            // 风量
            float fl = Float.parseFloat(item.getBkFl());
            maxFl = Math.max(maxFl, fl);
            minFl = Math.min(minFl, fl);
            flList.add(fl);
            // 风压
            if (StringUtils.isNotEmpty(item.getBkFjjy())) {
                if(!item.getBkFjjy().equals("--")) {
                    float fyF = Float.parseFloat(item.getBkFjjy());
                    fyFList.add(fyF);
                }else {
                    fyFList.add(0.0f);
                }
            }else {
                fyFList.add(0.0f);
            }
            // 功率
            float glF = Float.parseFloat(StringUtils.isNotEmpty(item.getBkDj1Zhgl()) ? item.getBkDj1Zhgl() : "0")
                    + Float.parseFloat(StringUtils.isNotEmpty(item.getBkDj2Zhgl()) ? item.getBkDj2Zhgl() : "0");
            glFList.add(glF);
            // 效率
            if (StringUtils.isNotEmpty(item.getBkFjjyxl())) {
                if(!item.getBkFjjyxl().equals("--")) {
                    float xlF = Float.parseFloat(item.getBkFjjyxl());
                    xlFList.add(Float.parseFloat(df2.format(xlF * 100)));
                }else{
                    xlFList.add(0.0f);
                }
            }else {
                xlFList.add(0.0f);
            }
        }
        //拟合点位生成
        JKalmanUtils.getInstance().release();
        fyNiheFList.addAll(JKalmanUtils.getInstance().correctList(fyFList));
        JKalmanUtils.getInstance().release();
        glNiheFList.addAll(JKalmanUtils.getInstance().correctList(glFList));
        JKalmanUtils.getInstance().release();
        xlNiheFList.addAll(JKalmanUtils.getInstance().correctList(xlFList));
        ChartLineDTO fyLineData = BezierCurveUtil.calPoints(flList, fyNiheFList, false);
        ChartLineDTO gllLineData = BezierCurveUtil.calPoints(flList, glNiheFList, false);
        ChartLineDTO xlLineData = BezierCurveUtil.calPoints(flList, xlNiheFList,  false);
        tfjChartRespDTO.setMaxFl(maxFl);
        tfjChartRespDTO.setMinFl(minFl);
        tfjChartRespDTO.setFy(fyLineData);
        tfjChartRespDTO.setGl(gllLineData);
        tfjChartRespDTO.setXl(xlLineData);
        tfjChartRespDTO.setFyNiheList(fyFList);
        tfjChartRespDTO.setGlNiheList(glFList);
        tfjChartRespDTO.setXlNiheList(xlFList);
        return tfjChartRespDTO;
    }

    /**
     * 根据id查询单条通风机任务数据
     *
     * @param dataTaskId
     */
    @Override
    public TfjTaskResqDTO selectTfjTaskDetail(Long dataTaskId) throws Exception {
        long startTime = System.currentTimeMillis();
        Future<DataTaskEntity> taskFuture = dataTaskService.selectDataTask(dataTaskId);
        Future<List<DataTfj>> resultListFuture = selectAllByDataTaskId(dataTaskId);
        TfjTaskResqDTO tfjTaskDTO = new TfjTaskResqDTO();
        boolean isGetDataTaskEntity = false;
        boolean isGetDataKyjEntityList = false;
        while (!isGetDataTaskEntity || !isGetDataKyjEntityList) {
            Thread.sleep(10);
            if (taskFuture.isDone() || taskFuture.isCancelled()) {
                isGetDataTaskEntity = true;
                if (taskFuture.isDone()) {
                    DataTaskEntity dataTaskEntity = taskFuture.get();
                    tfjTaskDTO.setDataTaskEntity(dataTaskEntity);
                }
            }
            if (resultListFuture.isDone() || resultListFuture.isCancelled()) {
                isGetDataKyjEntityList = true;
                if (resultListFuture.isDone()) {
                    List<DataTfj> dataKyjEntityList = resultListFuture.get();
                    dataKyjEntityList.forEach(item -> {
                        item.setKqmdzhhxsh(BiaokuangUtils.kongQiMiDuZhuanHuanXSFunc(item.getKqmd()));
                        item.setZhszhhxsh(BiaokuangUtils.zhuanSuZhuanHuanXSFunc(item.getEdzhs(), item.getSczhs()));
                    });
                    if (!CollectionUtils.isEmpty(dataKyjEntityList)) {
                        tfjTaskDTO.getResultList().addAll(dataKyjEntityList);
                    }

                }
            }
        }
        System.out.println(dataTaskId + " tfjTaskDTO = 通风机测试数据数据查询用时 " + (System.currentTimeMillis() - startTime));
        return tfjTaskDTO;
    }

    /**
     * 批量新增通风机测试结果数据
     *
     * @param dataTfjs
     */
    @Override
    public int batchDataTfj(List<DataTfj> dataTfjs) {
        return dataTfjMapper.batchDataTfj(dataTfjs);
    }

    /**
     * 批量更新通风机测试数据
     *
     * @param dataTfjs
     */
    @Override
    public int batchUpdateDataTfj(List<DataTfj> dataTfjs) {
        return dataTfjMapper.batchUpdateDataTfj(dataTfjs);
    }

    /**
     * 通风机数据导入报告
     *
     * @param tfjReportReqDTO
     */
    @Override
    public String dataTfjImportReport(TfjReportReqDTO tfjReportReqDTO) {
        // 报告模板名称
        String template = tfjReportReqDTO.getTempId();
        //测试任务信息
        DataTaskEntity dataTaskEntity = dataTaskService.selectDataTaskByDataTaskId(tfjReportReqDTO.getDataTaskEntity().getDataTaskId());
        tfjReportReqDTO.setDataTaskEntity(dataTaskEntity);
        //获取生成的工况图并转换png格式
        String rootPath = RuoYiConfig.getProfile() + RuoYiConfig.getChartPath() + "/";
        //工况图路径
        String gkImg = System.currentTimeMillis() + "-工况图.png";
        if(StringUtils.isNotEmpty(tfjReportReqDTO.getChart())) {
            boolean res = ProcessPictureUtils.convertToPng(tfjReportReqDTO.getChart(), rootPath + tfjReportReqDTO.getDataTaskEntity().getDataTaskId() + "/" + gkImg);
            String resString = res ? "成功" : "失败";
            System.out.println("工况图导入:" + resString);
        }
        // 曲线图的长宽
        int picW = 450;
        int picH = 250;
        //导入报告中
        Map<String, Object> params = new HashMap<>();
        //基本检测信息
        if(null != tfjReportReqDTO.getDataTaskEntity()) {
            params.put("受检单位", tfjReportReqDTO.getDataTaskEntity().getShjdw());
            params.put("设备名称", tfjReportReqDTO.getDataTaskEntity().getShbbh());
            params.put("主检姓名", tfjReportReqDTO.getDataTaskEntity().getZhjxm());
            params.put("测试人员", tfjReportReqDTO.getDataTaskEntity().getCshry());
            params.put("检测日期", DateUtils.dateTime(tfjReportReqDTO.getDataTaskEntity().getTaskCreateTime()));
        }
        //通风机参数
        if(null != tfjReportReqDTO.getDataTfjList() && !tfjReportReqDTO.getDataTfjList().isEmpty()) {
            DecimalFormat df3 = new DecimalFormat("####0.0");
            DecimalFormat df4 = new DecimalFormat("####0.0000");
            //通风机技术性能测定数据
            List<Map<String, Object>> xnList = new ArrayList<>();
            List<Map<String, Object>> xbList = new ArrayList<>();
            List<DataTfj> dataTfjs = tfjReportReqDTO.getDataTfjList();
            for(int i = 0 ; i < dataTfjs.size(); i++) {
                Map<String, Object> xnMap = new HashMap<>();
                xnMap.put("序号", String.valueOf(i + 1));
                xnMap.put("温度", StringUtils.isEmpty(dataTfjs.get(i).getWd()) ? "-" : dataTfjs.get(i).getWd());
                xnMap.put("大气压力", StringUtils.isEmpty(dataTfjs.get(i).getDqy()) ? "-" : dataTfjs.get(i).getDqy());
                xnMap.put("相对静压", StringUtils.isEmpty(dataTfjs.get(i).getXdjy()) ? "-" : dataTfjs.get(i).getXdjy());
                xnMap.put("风速", StringUtils.isEmpty(dataTfjs.get(i).getPjfs()) ? "-" : dataTfjs.get(i).getPjfs());
                xnMap.put("风量", StringUtils.isEmpty(dataTfjs.get(i).getFl()) ? "-" : dataTfjs.get(i).getFl());
                xnMap.put("湿度", StringUtils.isEmpty(dataTfjs.get(i).getShd()) ? "-" : dataTfjs.get(i).getShd());
                if (dataTfjs.get(i).getCshff().equals("静压全压法")) {
                    xnMap.put("差压", StringUtils.isEmpty(dataTfjs.get(i).getCddy()) ? "-" : dataTfjs.get(i).getCddy());
                } else if (dataTfjs.get(i).getCshff().equals("静压差法")) {
                    xnMap.put("差压", StringUtils.isEmpty(dataTfjs.get(i).getChy()) ? "-" : dataTfjs.get(i).getChy());
                }else{
                    xnMap.put("差压", "-");
                }
                xnMap.put("1号电机功率", StringUtils.isEmpty(dataTfjs.get(i).getDj1Djgl()) ? "-" : dataTfjs.get(i).getDj1Djgl());
                xnMap.put("2号电机功率", StringUtils.isEmpty(dataTfjs.get(i).getDj2Djgl()) ? "-" : dataTfjs.get(i).getDj2Djgl());
                if(StringUtils.isEmpty(dataTfjs.get(i).getDj1Chdxl()) || StringUtils.isEmpty(dataTfjs.get(i).getDj2Chdxl())) {
                    xnMap.put("传动效率", "-");
                }else {
                    xnMap.put("传动效率", BigDecimalUtils.valueToPercent(((Integer.valueOf(dataTfjs.get(i).getDj1Chdxl()) + Integer.valueOf(dataTfjs.get(i).getDj2Chdxl())) / 2) + ""));
                }
                if(StringUtils.isEmpty(dataTfjs.get(i).getDj1Chdxl()) || StringUtils.isEmpty(dataTfjs.get(i).getDj2Chdxl())) {
                    xnMap.put("额定功率", "-");
                }else {
                    xnMap.put("额定功率", df3.format(Float.parseFloat(dataTfjs.get(i).getDj1Edgl()) + Float.parseFloat(dataTfjs.get(i).getDj2Edgl())));
                }
                xnMap.put("测定方法", StringUtils.isEmpty(dataTfjs.get(i).getCshff()) ? "-" : dataTfjs.get(i).getCshff());
                xnList.add(xnMap);
                Map<String, Object> xbMap = new HashMap<>();
                xbMap.put("序号", String.valueOf(i + 1));
                xbMap.put("风机静压", StringUtils.isEmpty(dataTfjs.get(i).getFjjy()) ? "-" : dataTfjs.get(i).getFjjy());
                xbMap.put("空气密度", StringUtils.isEmpty(dataTfjs.get(i).getKqmd()) ? "-" : dataTfjs.get(i).getKqmd());
                if (dataTfjs.get(i).getCshff().equals("静压差法")) {
                    xbMap.put("测风面积", "-");
                } else {
                    xbMap.put("测风面积", StringUtils.isEmpty(dataTfjs.get(i).getCfmj()) ? "-" : dataTfjs.get(i).getCfmj());
                }
                if (dataTfjs.get(i).getCshff().equals("静压差法")) {
                    xbMap.put("静压测点面积", StringUtils.isEmpty(dataTfjs.get(i).getCfmj()) ? "-" : dataTfjs.get(i).getCfmj());
                } else {
                    xbMap.put("静压测点面积", StringUtils.isEmpty(dataTfjs.get(i).getJycdmj()) ? "-" : dataTfjs.get(i).getJycdmj());
                }
                xbMap.put("测压面积1", StringUtils.isEmpty(dataTfjs.get(i).getCymjx()) ? "-" : dataTfjs.get(i).getCymjx());
                xbMap.put("测压面积2", StringUtils.isEmpty(dataTfjs.get(i).getCymjd()) ? "-" : dataTfjs.get(i).getCymjd());
                xbMap.put("风机全压", StringUtils.isEmpty(dataTfjs.get(i).getFjqy()) ? "-" : dataTfjs.get(i).getFjqy());
                xbMap.put("静压输出功率", StringUtils.isEmpty(dataTfjs.get(i).getFjjygl()) ? "-" : dataTfjs.get(i).getFjjygl());
                xbMap.put("全压输出功率", StringUtils.isEmpty(dataTfjs.get(i).getFjqygl()) ? "-" : dataTfjs.get(i).getFjqygl());
                xbMap.put("风机运行效率", StringUtils.isEmpty(dataTfjs.get(i).getFjyxxl()) ? "" : df3.format(Float.parseFloat(BigDecimalUtils.valueToPercent(dataTfjs.get(i).getFjyxxl()))));
                xbMap.put("风机噪声", "-");
                xbMap.put("扩散器噪声", "-");
                xbList.add(xbMap);
            }
            //通风机技术性能测定现场实测数据
            List<Map<String, Object>> xnBkList = new ArrayList<>();
            List<Map<String, Object>> xbBkList = new ArrayList<>();
            for(int i = 0 ; i < dataTfjs.size(); i++) {
                Map<String, Object> xnBkMap = new HashMap<>();
                xnBkMap.put("工况点", String.valueOf(i + 1));
                xnBkMap.put("风量", StringUtils.isEmpty(dataTfjs.get(i).getFl()) ? "-" : dataTfjs.get(i).getFl());
                xnBkMap.put("风机装置全压", StringUtils.isEmpty(dataTfjs.get(i).getFjqy()) ? "-" : dataTfjs.get(i).getFjqy());
                xnBkMap.put("风机装置静压", StringUtils.isEmpty(dataTfjs.get(i).getFjjy()) ? "-" : dataTfjs.get(i).getFjjy());
                if (StringUtils.isEmpty(dataTfjs.get(i).getDj1Zhgl()) || StringUtils.isEmpty(dataTfjs.get(i).getDj2Zhgl())) {
                    xnBkMap.put("风机装置输入功率", "-");
                } else {
                    float dj1zhgl = Float.parseFloat(dataTfjs.get(i).getDj1Zhgl());
                    float dj2zhgl = Float.parseFloat(dataTfjs.get(i).getDj2Zhgl());
                    xnBkMap.put("风机装置输入功率", df3.format(dj1zhgl + dj2zhgl));
                }
                xnBkMap.put("全压输出功率", StringUtils.isEmpty(dataTfjs.get(i).getFjqygl()) ? "-" : dataTfjs.get(i).getFjqygl());
                xnBkMap.put("静压输出功率", StringUtils.isEmpty(dataTfjs.get(i).getFl()) ? "-" : dataTfjs.get(i).getFl());
                xnBkMap.put("全压效率", StringUtils.isEmpty(dataTfjs.get(i).getFjqyxl()) ? "-" : df3.format(Float.parseFloat(BigDecimalUtils.valueToPercent(dataTfjs.get(i).getFjqyxl()))));
                xnBkMap.put("静压效率", StringUtils.isEmpty(dataTfjs.get(i).getFjjyxl()) ? "-" : df3.format(Float.parseFloat(BigDecimalUtils.valueToPercent(dataTfjs.get(i).getFjjyxl()))));
                xnBkMap.put("实测转速", StringUtils.isEmpty(dataTfjs.get(i).getSczhs()) ? "-" : df3.format(Float.parseFloat(dataTfjs.get(i).getSczhs())));
                if (StringUtils.isEmpty(dataTfjs.get(i).getKqmdzhhxsh())) {
                    dataTfjs.get(i).setKqmdzhhxsh(BiaokuangUtils.kongQiMiDuZhuanHuanXSFunc(dataTfjs.get(i).getKqmd()));
                }
                xnBkMap.put("空气密度换算系数", StringUtils.isEmpty(dataTfjs.get(i).getKqmdzhhxsh()) ? "-" : df4.format(Float.parseFloat(dataTfjs.get(i).getKqmdzhhxsh())));
                xnBkList.add(xnBkMap);
                Map<String, Object> xbBkMap = new HashMap<>();
                xbBkMap.put("工况点", String.valueOf(i + 1));
                if (StringUtils.isEmpty(dataTfjs.get(i).getZhszhhxsh())) {
                    dataTfjs.get(i).setZhszhhxsh(BiaokuangUtils.zhuanSuZhuanHuanXSFunc(dataTfjs.get(i).getEdzhs(), dataTfjs.get(i).getSczhs()));
                }
                if (StringUtils.isEmpty(dataTfjs.get(i).getZhszhhxsh())) {
                    xbBkMap.put("kn", "-");
                    xbBkMap.put("kn2", "-");
                    xbBkMap.put("kn3", "-");
                } else {
                    float zhszhhxsh = Float.parseFloat(dataTfjs.get(i).getZhszhhxsh());
                    xbBkMap.put("kn", df4.format(zhszhhxsh));
                    xbBkMap.put("kn2", df4.format(zhszhhxsh * zhszhhxsh));
                    xbBkMap.put("kn3", df4.format(zhszhhxsh * zhszhhxsh * zhszhhxsh));
                }
                xbBkMap.put("标况风量", StringUtils.isEmpty(dataTfjs.get(i).getBkFl()) ? "-" : df3.format(Float.parseFloat(dataTfjs.get(i).getBkFl())));
                xbBkMap.put("标况全压", StringUtils.isEmpty(dataTfjs.get(i).getBkFjqy()) ? "-" : df3.format(Float.parseFloat(dataTfjs.get(i).getBkFjqy())));
                xbBkMap.put("标况静压", StringUtils.isEmpty(dataTfjs.get(i).getBkFjjy()) ? "-" : df3.format(Float.parseFloat(dataTfjs.get(i).getBkFjjy())));
                if (StringUtils.isEmpty(dataTfjs.get(i).getBkDj1Zhgl()) || StringUtils.isEmpty(dataTfjs.get(i).getBkDj2Zhgl())) {
                    xbBkMap.put("标况输入功率", "-");
                } else {
                    float dj1zhgl = Float.parseFloat(dataTfjs.get(i).getBkDj1Zhgl());
                    float dj2zhgl = Float.parseFloat(dataTfjs.get(i).getBkDj2Zhgl());
                    xbBkMap.put("标况输入功率", df3.format(dj1zhgl + dj2zhgl));
                }
                xbBkMap.put("标况全压功率", StringUtils.isEmpty(dataTfjs.get(i).getBkFjqygl()) ? "-" : df3.format(Float.parseFloat(dataTfjs.get(i).getBkFjqygl())));
                xbBkMap.put("标况静压功率", StringUtils.isEmpty(dataTfjs.get(i).getBkFjjygl()) ? "-" : df3.format(Float.parseFloat(dataTfjs.get(i).getBkFjjygl())));
                xbBkMap.put("标况全压效率", StringUtils.isEmpty(dataTfjs.get(i).getBkFjqyxl()) ? "-" : df3.format(Float.parseFloat(BigDecimalUtils.valueToPercent(dataTfjs.get(i).getBkFjqyxl()))));
                xbBkMap.put("标况静压功率", StringUtils.isEmpty(dataTfjs.get(i).getBkFjjyxl()) ? "-" : df3.format(Float.parseFloat(BigDecimalUtils.valueToPercent(dataTfjs.get(i).getBkFjjyxl()))));
                xbBkList.add(xbBkMap);
            }
            //list结果传到map
            params.put("性能测试数据List", xnList);
            params.put("性能测试数据续List", xbList);
            params.put("标况测试数据List", xnBkList);
            params.put("标况测试数据续List", xbBkList);
        }
        //工况图
        params.put("工况图", new PictureRenderData(picW, picH, rootPath + tfjReportReqDTO.getDataTaskEntity().getDataTaskId() + "/" +gkImg));
        String docRootPath = RuoYiConfig.getProfile() + RuoYiConfig.getDocumentPath() + "/";
        String docFileName = System.currentTimeMillis() + "-tfj.docx";
        boolean isOk = ExportWordUtils.exportWord(RuoYiConfig.getProfile() + RuoYiConfig.getTemplatePath() + "/" + template, docRootPath, docFileName, params);
        System.out.println("isOK: 生成测试报告" + isOk + " docFileName: " + docFileName);
        return "/profile/document/" + docFileName;
    }

    /**
     * 通风机风量变更
     *
     * @param modifyTfj
     */
    @Override
    public Map<String, String> modifyTfjFl(DataTfj modifyTfj) {
        //工况风量
        String fl = GongkuangUtils.fengLiangFunc(modifyTfj);
        //标况风量
        String bkFl = BiaokuangUtils.fengLiangFunc(modifyTfj);
        Map<String, String> params = new HashMap<>();
        params.put("fl", fl);
        params.put("bkFl", bkFl);
        return params;
    }

    /**
     * 通风机饱和蒸气压
     *
     * @param wd
     */
    @Override
    public String modifyTfjBhzqy(String wd) {
        return GongkuangUtils.baoHeZhengQiYaFunc(wd);
    }

    /**
     * 通风机空气密度计算
     */
    @Override
    public String modifyTfjKqmd(DataTfj modifyTfj) {
        return GongkuangUtils.kongQiMiDuFunc(modifyTfj);
    }

    /**
     * 通风机轴功率计算
     */
    @Override
    public Map<String, String> modifyTfjZgl(DataTfj modifyTfj) {
        String zgl = GongkuangUtils.zhouGongLv1Func(modifyTfj);
        String bkZgl = BiaokuangUtils.zhouGonglv1Func(modifyTfj);
        Map<String, String> params = new HashMap<>();
        params.put("zgl", zgl);
        params.put("bkZgl", bkZgl);
        return params;
    }

    /**
     * 通风机风机装置静压计算
     *
     */
    @Override
    public String modityTfjJy(DataTfj modifyTfj) {
        return GongkuangUtils.fengJiZhuangZhiJYFunc(modifyTfj);
    }

    /**
     * 通风机风机装置全压计算
     *
     * @param modifyTfj
     */
    @Override
    public String modifyTfjQy(DataTfj modifyTfj) {
        return GongkuangUtils.fengJiZhuangZhiQYFunc(modifyTfj);
    }

    /**
     * 通风机风机装置功率计算
     *
     * @param modifyTfj
     */
    @Override
    public Map<String, String> modifyTfjGl(DataTfj modifyTfj) {
        //风机装置静压功率计算
        String jygl = GongkuangUtils.fengJiZhuangZhiJYGLFunc(modifyTfj);
        //风机装置全压功率计算
        String qygl = GongkuangUtils.fengJiZhuangZhiQYGLFunc(modifyTfj);
        modifyTfj.setFjqygl(qygl);
        //标况全压功率计算
        String bkqygl = BiaokuangUtils.fengJiZhuangZhiQYGLFunc(modifyTfj);
        Map<String, String> params = new HashMap<>();
        params.put("jygl", jygl);
        params.put("qygl", qygl);
        params.put("bkqygl", bkqygl);
        return params;
    }

    /**
     * 通风机风机装置效率计算
     *
     * @param modifyTfj
     */
    @Override
    public Map<String, String> modifyTfjXl(DataTfj modifyTfj) {
        //风机装置静压效率计算
        String jyxl = GongkuangUtils.fengJiZhuangZhiJYXLFunc(modifyTfj);
        //风机装置全压效率计算
        String qyxl = GongkuangUtils.fengJiZhuangZhiQYXLFunc(modifyTfj);
        //风机装置标况全压效率计算
        String bkqyxl = BiaokuangUtils.fengJiZhuangZhiQYXLFunc(modifyTfj);
        Map<String, String> params = new HashMap<>();
        params.put("jyxl", jyxl);
        params.put("qyxl", qyxl);
        params.put("bkqyxl", bkqyxl);
        return params;
    }

    /**
     * 通风机风机运行效率和工序能耗
     *
     * @param modifyTfj
     */
    @Override
    public Map<String, String> modifyTfjXlAndNh(DataTfj modifyTfj) {
        //根据通风方式获取不同运行效率
        String yxxl = GongkuangUtils.fengJiYunXingXiaoLvFunc(modifyTfj);
        //工序能耗
        String gxnh = GongkuangUtils.gongXvNengHaoFunc(yxxl);
        Map<String, String> params = new HashMap<>();
        params.put("yxxl", yxxl);
        params.put("gxnh", gxnh);
        return params;
    }

    /**
     * 通风机风速计算
     *
     * @param modifyTfj
     */
    @Override
    public String modifyTfjFs(DataTfj modifyTfj) {
        return GongkuangUtils.fengSuFunc(modifyTfj);
    }

    /**
     * 通风机组效率
     *
     * @param modifyTfj
     */
    @Override
    public String modifyTfjzXl(DataTfj modifyTfj) {
        return GongkuangUtils.tongFengJiZuXlFunc(modifyTfj);
    }

    @Override
    public DataTfj modifyTfjData(DataTfj dataTfj) {
        //电机公式计算
        //平均电压计算、平均电流计算、功率因数计算
        dataTfj = getPower(dataTfj, dataTfj.getTfjId());
        dataTfjMapper.updateDataTfj(dataTfj);
        //根据变更参数计算，按照顺序
        //工况计算 依次计算 顺序不能乱
        dataTfj.setBhzhqy(GongkuangUtils.baoHeZhengQiYaFunc(dataTfj.getWd()));
        dataTfj.setKqmd(GongkuangUtils.kongQiMiDuFunc(dataTfj));
        dataTfj.setFl(GongkuangUtils.fengLiangFunc(dataTfj));
        dataTfj.setCddy(GongkuangUtils.ceFengDianDongYaFunc(dataTfj));
        dataTfj.setKsqchkdy(GongkuangUtils.kuoSanQiChuKouDYFunc(dataTfj));
        dataTfj.setDj1Zhgl(GongkuangUtils.zhouGongLv1Func(dataTfj));
        dataTfj.setDj2Zhgl(GongkuangUtils.zhouGongLv2Func(dataTfj));
        dataTfj.setFjjy(GongkuangUtils.fengJiZhuangZhiJYFunc(dataTfj));
        dataTfj.setFjqy(GongkuangUtils.fengJiZhuangZhiQYFunc(dataTfj));
        dataTfj.setFjjygl(GongkuangUtils.fengJiZhuangZhiJYGLFunc(dataTfj));
        dataTfj.setFjqygl(GongkuangUtils.fengJiZhuangZhiQYGLFunc(dataTfj));
        dataTfj.setFjjyxl(GongkuangUtils.fengJiZhuangZhiJYXLFunc(dataTfj));
        dataTfj.setFjqyxl(GongkuangUtils.fengJiZhuangZhiQYXLFunc(dataTfj));
        dataTfj.setFjyxxl(GongkuangUtils.fengJiYunXingXiaoLvFunc(dataTfj));
        dataTfj.setGxnh(GongkuangUtils.gongXvNengHaoFunc(dataTfj.getFjyxxl()));
        dataTfj.setTfjzxl(GongkuangUtils.tongFengJiZuXlFunc(dataTfj));
        //标况计算 依次计算 顺序不能乱
        dataTfj.setZhszhhxsh(BiaokuangUtils.zhuanSuZhuanHuanXSFunc(dataTfj.getEdzhs(), dataTfj.getSczhs()));
        dataTfj.setKqmdzhhxsh(BiaokuangUtils.kongQiMiDuZhuanHuanXSFunc(dataTfj.getKqmd()));
        dataTfj.setBkFl(BiaokuangUtils.fengLiangFunc(dataTfj));
        dataTfj.setBkDj1Zhgl(BiaokuangUtils.zhouGonglv1Func(dataTfj));
        dataTfj.setBkDj2Zhgl(BiaokuangUtils.zhouGonglv2Func(dataTfj));
        dataTfj.setBkFjjy(BiaokuangUtils.fengJiZhuangZhiJYFunc(dataTfj));
        dataTfj.setBkFjqy(BiaokuangUtils.fengJiZhuangZhiQYFunc(dataTfj));
        dataTfj.setBkFjjygl(BiaokuangUtils.fengJiZhuangZhiJYGLFunc(dataTfj));
        dataTfj.setBkFjqygl(BiaokuangUtils.fengJiZhuangZhiQYGLFunc(dataTfj));
        dataTfj.setBkFjjyxl(BiaokuangUtils.fengJiZhuangZhiJYXLFunc(dataTfj));
        dataTfj.setBkFjqyxl(BiaokuangUtils.fengJiZhuangZhiQYXLFunc(dataTfj));
        dataTfjMapper.updateDataTfj(dataTfj);
        return dataTfj;
    }

    /**
     * 根据任务id变更通风机状态
     *
     * @param dataTaskId
     */
    @Override
    public int updateDataTfjByDataTaskId(Long dataTaskId) {
        //变更任务状态
        try {
            Long[] dataTaskIds = new Long[] {dataTaskId};
            dataTaskService.deleteDataTaskByDataTaskIds(dataTaskIds, "003");
        }catch (Exception e){
            e.printStackTrace();
        }
        return dataTfjMapper.updateDataTfjByDataTaskId(dataTaskId);
    }

    /**
     * 根据通风机id变更任务状态
     *
     * @param tfjId
     */
    @Override
    public int updateDataTfjByTfjId(Long tfjId) {
        return dataTfjMapper.updateDataTfjByTfjId(tfjId);
    }

    /**
     * 计算电机
     */
    private DataTfj getPower(DataTfj dataTfjRes, Long tfjId) {
        //根据测试项id获取详情
        DataTfj dataTfj = dataTfjMapper.selectDataTfjByTfjId(tfjId);
        //电机1A相电流
        float dj1aA = dataTfj.getDj1Aa().isEmpty() ? 0f : Float.parseFloat(dataTfj.getDj1Aa());
        //电机1B相电流
        float dj1bA = dataTfj.getDj1Ab().isEmpty() ? 0f : Float.parseFloat(dataTfj.getDj1Ab());
        //电机1C相电流
        float dj1cA = dataTfj.getDj1Ac().isEmpty() ? 0f : Float.parseFloat(dataTfj.getDj1Ac());
        //电机1AB线电压
        float dj1abV = dataTfj.getDj1Uab().isEmpty() ? 0f : Float.parseFloat(dataTfj.getDj1Uab());
        //电机1CA线电压
        float dj1caV = dataTfj.getDj1Uca().isEmpty() ? 0f : Float.parseFloat(dataTfj.getDj1Uca());
        //电机1BC线电压
        float dj1bcV = dataTfj.getDj1Ubc().isEmpty() ? 0f : Float.parseFloat(dataTfj.getDj1Ubc());
        //电机1平均电流
        float dj1pjdl = dataTfj.getDj1Pjdl().isEmpty() ? 0f : Float.parseFloat(dataTfj.getDj1Pjdl());
        //电机1平均电压
        float dj1pjdy = dataTfj.getDj1Pjdy().isEmpty() ? 0f : Float.parseFloat(dataTfj.getDj1Pjdy());
        //电机1查看变更的参数
        if(StringUtils.isNotEmpty(dataTfjRes.getDj1Pjdl())) {
            //电机1变更平均电流
            float dj1diffDL = Float.parseFloat(dataTfjRes.getDj1Pjdl()) - dj1pjdl;
            dataTfj.setDj1Aa(String.valueOf(dj1aA + dj1diffDL));
            dataTfj.setDj1Ab(String.valueOf(dj1bA + dj1diffDL));
            dataTfj.setDj1Ac(String.valueOf(dj1cA + dj1diffDL));
            dataTfj.setDj1Pjdl(dataTfjRes.getDj1Pjdl());
        }
        if(StringUtils.isNotEmpty(dataTfjRes.getDj1Pjdy())) {
            //电机1变更平均电压
            float dj1diffDY = Float.parseFloat(dataTfjRes.getDj1Pjdy()) - dj1pjdy;
            dataTfj.setDj1Uab(String.valueOf(dj1abV + dj1diffDY));
            dataTfj.setDj1Uca(String.valueOf(dj1caV + dj1diffDY));
            dataTfj.setDj1Ubc(String.valueOf(dj1bcV + dj1diffDY));
            dataTfj.setDj1Pjdy(dataTfjRes.getDj1Pjdy());
        }
        if(StringUtils.isNotEmpty(dataTfjRes.getDj1Glysh())) {
            //电机1变更功率因数
            dataTfj.setDj1Glysh(dataTfjRes.getDj1Glysh());
        }
        // 电机1如果功率因数为0 ，则赋值为1 ，否则无法计算
        if (dataTfjRes.getDj1Glysh() == null || dataTfjRes.getDj1Glysh().isEmpty() || Float.parseFloat(dataTfjRes.getDj1Glysh()) == 0) {
            dataTfj.setDj1Glysh(1 + "");
        }
        float dj1szgl = ((Float.parseFloat(dataTfj.getDj1Aa()) * Float.parseFloat(dataTfj.getDj1Uab())) + (Float.parseFloat(dataTfj.getDj1Ab()) * Float.parseFloat(dataTfj.getDj1Ubc())) + (Float.parseFloat(dataTfj.getDj1Ac()) * Float.parseFloat(dataTfj.getDj1Uca()))) / 1000;
        //计算公式是通过平均电流乘以平均电压乘以3
        //float dj1szgl = (Float.parseFloat(dataTfjRes.getDj1Pjdl()) * Float.parseFloat(dataTfjRes.getDj1Pjdy())) * 3 /1000;
        float dj1djgl = (dj1szgl * Float.parseFloat(dataTfj.getDj1Glysh())) / 1.732F;
        dataTfj.setDj1Djgl(String.valueOf(dj1djgl));
        MotorUtils motorUtils = new MotorUtils(dataTfj.getDj1Uab(), dataTfj.getDj1Ubc(), dataTfj.getDj1Uca(), dataTfj.getDj1Aa(), dataTfj.getDj1Ab(), dataTfj.getDj1Ac(), dataTfj.getDj1Pjdy(), dataTfj.getDj1Pjdl());
        Map<String, String> resMap = motorUtils.cal(dataTfjRes.getDj1Pjdy(), dataTfjRes.getDj1Pjdl(), dataTfjRes.getDj1Glysh(), dataTfj.getDj1Chdxl(), dataTfj.getDj1Eddy(), dataTfj.getDj1Eddl(), dataTfj.getDj1Edgl(), dataTfj.getDj1Edxl(), dataTfj.getDj1Kzdl(), dataTfj.getDj1Kzgl(), dataTfj.getDj1Edglysh(), dataTfj.getDj1Jsh(), dataTfj.getDj1Wgjjdl());
        dataTfj.setDj1Yxzht(resMap.get("运行状态"));
        dataTfj.setDj1Zhgl(resMap.get("轴功率"));
        dataTfj.setDj1Djxl(resMap.get("电机效率"));
        dataTfj.setDj1Zhxl(String.valueOf(Float.parseFloat(resMap.get("综合效率")) / 100f));
        dataTfj.setDj1Fzxsh(resMap.get("负载系数"));
//        float dj1eddy = Float.parseFloat(dataTfj.getDj1Edgl());
//        dataTfj.setDj1Shchgl(String.valueOf(dj1eddy * Float.parseFloat(dataTfj.getDj1Fzxsh())));
        dataTfj.setDj1Shchgl(resMap.get("输出功率"));
        //电机2A相电流
        float dj2aA = dataTfj.getDj2Aa().isEmpty() ? 0f : Float.parseFloat(dataTfj.getDj2Aa());
        //电机2B相电流
        float dj2bA = dataTfj.getDj2Ab().isEmpty() ? 0f : Float.parseFloat(dataTfj.getDj2Ab());
        //电机2C相电流
        float dj2cA = dataTfj.getDj2Ac().isEmpty() ? 0f : Float.parseFloat(dataTfj.getDj2Ac());
        //电机2AB线电压
        float dj2abV = dataTfj.getDj2Uab().isEmpty() ? 0f : Float.parseFloat(dataTfj.getDj2Uab());
        //电机2CA线电压
        float dj2caV = dataTfj.getDj2Uca().isEmpty() ? 0f : Float.parseFloat(dataTfj.getDj2Uca());
        //电机2BC线电压
        float dj2bcV = dataTfj.getDj2Ubc().isEmpty() ? 0f : Float.parseFloat(dataTfj.getDj2Ubc());
        //电机2平均电流
        float dj2pjdl = dataTfj.getDj2Pjdl().isEmpty() ? 0f : Float.parseFloat(dataTfj.getDj2Pjdl());
        //电机2平均电压
        float dj2pjdy = dataTfj.getDj2Pjdy().isEmpty() ? 0f : Float.parseFloat(dataTfj.getDj2Pjdy());
        //电机2查看变更的参数
        if(StringUtils.isNotEmpty(dataTfjRes.getDj2Pjdl())) {
            //电机2变更平均电流
            float dj2diffDL = Float.parseFloat(dataTfjRes.getDj2Pjdl()) - dj2pjdl;
            dataTfj.setDj2Aa(String.valueOf(dj2aA + dj2diffDL));
            dataTfj.setDj2Ab(String.valueOf(dj2bA + dj2diffDL));
            dataTfj.setDj2Ac(String.valueOf(dj2cA + dj2diffDL));
            dataTfj.setDj2Pjdl(dataTfjRes.getDj1Pjdl());
        }
        if(StringUtils.isNotEmpty(dataTfjRes.getDj2Pjdy())) {
            //电机2变更平均电压
            float dj2diffDY = Float.parseFloat(dataTfjRes.getDj2Pjdy()) - dj2pjdy;
            dataTfj.setDj2Uab(String.valueOf(dj2abV + dj2diffDY));
            dataTfj.setDj2Uca(String.valueOf(dj2caV + dj2diffDY));
            dataTfj.setDj2Ubc(String.valueOf(dj2bcV + dj2diffDY));
            dataTfj.setDj2Pjdy(dataTfjRes.getDj2Pjdy());
        }
        if(StringUtils.isNotEmpty(dataTfjRes.getDj2Glysh())) {
            //电机2变更功率因数
            dataTfj.setDj2Glysh(dataTfjRes.getDj2Glysh());
        }
        // 电机2如果功率因数为0 ，则赋值为1 ，否则无法计算
        if (dataTfjRes.getDj2Glysh() == null || dataTfjRes.getDj2Glysh().isEmpty() || Float.parseFloat(dataTfjRes.getDj2Glysh()) == 0) {
            dataTfj.setDj2Glysh(1 + "");
        }
        float dj2szgl = ((Float.parseFloat(dataTfj.getDj2Aa()) * Float.parseFloat(dataTfj.getDj2Uab())) + (Float.parseFloat(dataTfj.getDj2Ab()) * Float.parseFloat(dataTfj.getDj2Ubc())) + (Float.parseFloat(dataTfj.getDj2Ac()) * Float.parseFloat(dataTfj.getDj2Uca()))) / 1000;
        //计算公式是通过平均电流乘以平均电压乘以3
        //float dj2szgl = (Float.parseFloat(dataTfjRes.getDj2Pjdl()) * Float.parseFloat(dataTfjRes.getDj2Pjdy())) * 3 /1000;
        float dj2djgl = (dj2szgl * Float.parseFloat(dataTfj.getDj2Glysh())) / 1.732F;
        dataTfj.setDj2Djgl(String.valueOf(dj2djgl));
        motorUtils = new MotorUtils(dataTfj.getDj2Uab(), dataTfj.getDj2Ubc(), dataTfj.getDj2Uca(), dataTfj.getDj2Aa(), dataTfj.getDj2Ab(), dataTfj.getDj2Ac(), dataTfj.getDj2Pjdy(), dataTfj.getDj2Pjdl());
        Map<String, String> dj2resMap = motorUtils.cal(dataTfjRes.getDj2Pjdy(), dataTfjRes.getDj2Pjdl(), dataTfjRes.getDj2Glysh(), dataTfj.getDj2Chdxl(), dataTfj.getDj2Eddy(), dataTfj.getDj2Eddl(), dataTfj.getDj2Edgl(), dataTfj.getDj2Edxl(), dataTfj.getDj2Kzdl(), dataTfj.getDj2Kzgl(), dataTfj.getDj2Edglysh(), dataTfj.getDj2Jsh(), dataTfj.getDj2Wgjjdl());
        dataTfj.setDj2Yxzht(dj2resMap.get("运行状态"));
        dataTfj.setDj2Zhgl(dj2resMap.get("轴功率"));
        dataTfj.setDj2Djxl(dj2resMap.get("电机效率"));
        dataTfj.setDj2Zhxl(String.valueOf(Float.parseFloat(dj2resMap.get("综合效率")) / 100f));
        dataTfj.setDj2Fzxsh(dj2resMap.get("负载系数"));
        //float dj2eddy = Float.parseFloat(dataTfj.getDj2Edgl());
        dataTfj.setDj2Shchgl(dj2resMap.get("输出功率"));
        return dataTfj;
    }
}
